<template>
    <div  class="inventory-container">
        
        <!-- 查询表单 -->
        <el-form :inline="true">
            <el-form-item>
                <el-select v-model="searchObj.fwhid" clearable placeholder="仓库">
                    <el-option
                    v-for="item in warehouseList"
                    :key="item.fwhid"
                    :label="item.fwhname"
                    :value="item.fwhid">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-autocomplete
                    v-model="searchObj.fmid"
                    :fetch-suggestions="querySearch"
                    :trigger-on-focus="false"
                    class="inline-input"
                    placeholder="物资编号"
                    value-key="fmid" />
            </el-form-item>
            <el-form-item>
                <el-autocomplete
                v-model="searchObj.fmname"
                :fetch-suggestions="querySearch"
                :trigger-on-focus="false"
                class="inline-input"
                placeholder="物资名称"
                value-key="fmname" />
            </el-form-item>
            <el-form-item>
                <el-select v-model="searchObj.fmkind" clearable placeholder="物资种类">
                    <el-option
                    v-for="item in kinds"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="fetchData()">查询</el-button>
                <el-button type="default" @click="resetData()">清空</el-button>
            </el-form-item>
        </el-form>
        
        <el-button type="primary" @click.native="vueCreate">新建库存信息</el-button>
        <inventoryinfo :dialogFormVisible="dialogFormVisible"  :updateOrCreate="updateOrCreate" :editStockMasterInfo="editStockMasterInfo" />
        <!-- 表单部分 -->
        <el-table
            :data="stockMasterList"
            style="width: 100%"
        >
            <el-table-column
                prop="fwhid"
                label="仓库编号"
                width="100"
                sortable
                />
            <el-table-column
                prop="fwhname"
                label="仓库名称"
                width="150"
                sortable
                />
            <el-table-column
                prop="fmid"
                label="物资编号"
                width="100"
                sortable
                />
            <el-table-column
                prop="fmname"
                label="物资名称"
                width="150"
                sortable
                />
            <el-table-column label="物资种类" width="100" prop="fmkind" sortable>
                <template slot-scope="scope">
                    <el-tag type="warning" effect="dark" v-if="scope.row.fmkind==1">设备</el-tag>
                    <el-tag type="danger" effect="dark" v-else-if="scope.row.fmkind==2">耗材</el-tag>
                    <el-tag type="danger" effect="dark" v-else>未知</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                prop="fsmqty"
                label="库存数量"
                width="100"
                sortable
                />
            <el-table-column
                prop="fsmsafestockqty"
                label="安全库存数量"
                width="150"
                sortable
                />
            <el-table-column
                prop="fsmmemo"
                label="备注"
                width="200"
                sortable
                />
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="big" @click="vueDelete(scope.row.fwhid,scope.row.fmid)">删除</el-button>
                    <el-button type="primary" size="big"  @click="vueEdit(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
import { getStockMasterList, deleteStockMaster} from '@/api/stockMaster'
import { getWarehouseList } from '@/api/warehouse'
import inventoryinfo from '@/views/warehouse/information/inventory/components/InventoryInfo'

export default {
    components: {
        inventoryinfo
    },
    data () {
        return {
            stockMasterList: {},
            searchObj: {},
            warehouseList: {},
            dialogFormVisible: false,
            /*true 代表更新，false代表创建*/
            updateOrCreate: false,
            editStockMasterInfo: {},
            kinds: [{id:"1",name:"设备"},{id:"2",name:"耗材"}]
            
        }
    },
    created() {
        this.fetchData()
        getWarehouseList({}).then(response => {
            this.warehouseList = response.data.warehouseList
        })
    },
    methods: {
        vueCreate() {
            this.dialogFormVisible=true
            this.updateOrCreate=false
        },
        vueEdit(row) {
            this.dialogFormVisible=true
            this.updateOrCreate=true
            this.editStockMasterInfo=row
        },
        vueReset() {
            this.dialogFormVisible=false
            this.updateOrCreate=false
            this.editStockMasterInfo={}
        },
        vueDelete(fwhid,fmid) {
            this.$confirm('此操作将永久删除，受否继续？','提示',{
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteStockMaster(fwhid,fmid).then(response => {
                    this.fetchData()
                    this.$message({
                        type: 'success',
                        message: '删除成功'
                    })
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        fetchData() {
            getStockMasterList(this.searchObj).then(response => {
                this.stockMasterList = response.data.stockMasterList
            })
        },
        resetData() {
            this.searchObj = {}
        }
    }

}
</script>
<style lang="scss" scoped>
    .inventory-container {
      margin: 10px 50px 0;
      overflow: hidden;
    }
</style>